<template>
  <Dialog :title="t('studentPatent.view.title')" v-model="dialogVisible" width="950">
    <el-descriptions v-loading="formLoading" :column="1" border label-width="230">
      <el-descriptions-item :label="t('studentPatent.fields.studentCode')">
        <EditInput
          v-model="formData.studentCode"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.studentCode')"
          :disabled="true"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.studentName')">
        <EditInput
          v-model="formData.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.studentName')"
          :disabled="true"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.mentorName')">
        <UserSelect
          v-model="formData.mentorId"
          @selected="handleMentorSelected"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.mentorName')"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.patentName')">
        <EditInput
          v-model="formData.patentName"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.patentName')"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.attachment')">
        <IMRUpload
          v-model="formData.attachment"
          :file-type="['pdf']"
          :file-size="20"
          :limit="1"
          :disabled="!edit"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.patentType')">
        <EditDict
          :dict-type="DICT_TYPE.RESEARCH_PATENT_TYPE"
          v-model="formData.patentType"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.patentType')"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.patentStatus')">
        <EditDict
          :dict-type="DICT_TYPE.RESEARCH_PATENT_STATUS"
          v-model="formData.patentStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.patentStatus')"
        />
      </el-descriptions-item>
      <el-descriptions-item
        v-if="formData.patentStatus === '已公开'"
        :label="t('studentPatent.fields.publishNumber')"
      >
        <EditInput
          v-model="formData.publishNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.publishNumber')"
        />
      </el-descriptions-item>
      <el-descriptions-item
        v-if="formData.patentStatus === '已公开'"
        :label="t('studentPatent.fields.publishTime')"
      >
        <EditDate
          v-model="formData.publishTime"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.publishTime')"
        />
      </el-descriptions-item>
      <el-descriptions-item
        v-if="formData.patentStatus === '已授权'"
        :label="t('studentPatent.fields.patentNumber')"
      >
        <EditInput
          v-model="formData.patentNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.patentNumber')"
        />
      </el-descriptions-item>
      <el-descriptions-item
        v-if="formData.patentStatus === '已授权'"
        :label="t('studentPatent.fields.patentTime')"
      >
        <EditDate
          v-model="formData.patentTime"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.patentTime')"
        />
      </el-descriptions-item>
      <el-descriptions-item key="personalRanking" :label="t('studentPatent.fields.personalRanking')">
        <EditDict
          :dict-type="DICT_TYPE.RESEARCH_PATENT_PERSONAL_RANK"
          v-model="formData.personalRanking"
          :placeholder="
            t('common.selectTextPlaceholder') + t('studentPatent.fields.personalRanking')
          "
        />
      </el-descriptions-item>
      <el-descriptions-item key="casRanking" :label="t('studentPatent.fields.casRanking')">
        <EditDict
          :dict-type="DICT_TYPE.RESEARCH_PATENT_USTC_RANK"
          v-model="formData.casRanking"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.casRanking')"
        />
      </el-descriptions-item>
      <el-descriptions-item key="imrRanking" :label="t('studentPatent.fields.imrRanking')">
        <EditDict
          :dict-type="DICT_TYPE.RESEARCH_PATENT_IMR_RANK"
          v-model="formData.imrRanking"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.imrRanking')"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.remark')">
        <EditInput
          v-model="formData.remark"
          type="textarea"
          :rows="2"
          :maxlength="200"
          show-word-limit
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.remark')"
        />
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.reviewStatus')">
        <el-tag v-if="formData.reviewStatus" type="primary">{{ formData.reviewStatus }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item :label="t('studentPatent.fields.reviewTime')">
        {{ formatDate(formData.reviewTime) }}
      </el-descriptions-item>
    </el-descriptions>
    <template v-if="edit" #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="edit = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
  <StudentPatentAudit ref="auditRef" @success="handleAuditFinish" />
</template>
<script setup lang="ts">
import { StudentPatentApi, StudentPatentVO } from '@/api/core/studentpatent'
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import EditDate from '@/views/core/ustcstudent/components/input/EditDate.vue'
import IMRUpload from '@/components/IMRUpload/index.vue'
import EditDict from '@/views/core/ustcstudent/components/input/EditDict.vue'
import EditInput from '@/views/core/ustcstudent/components/input/EditInput.vue'
import UserSelect from '@/views/system/user/components/UserSelect.vue'
import { UserVO } from '@/api/system/user'
import StudentPatentAudit from './StudentPatentAudit.vue'

/** 专利 表单 */
defineOptions({ name: 'StudentPatentForm' })

const edit = ref(false)
provide('editMode', edit)

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref<Partial<StudentPatentVO>>({})

const auditRef = ref<InstanceType<typeof StudentPatentAudit>>()

/** 打开弹窗 */
const open = async (id: number) => {
  edit.value = false
  dialogVisible.value = true
  formLoading.value = true
  formData.value = {}
  try {
    formData.value = await StudentPatentApi.getStudentPatent(id)
    if (formData.value && !formData.value?.attachment) {
      formData.value.attachment = '[]'
    }
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as StudentPatentVO
    await StudentPatentApi.updateStudentPatent(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const handleEdit = () => {
  edit.value = true
}

const handleAudit = () => {
  auditRef.value?.open(formData.value.id!)
}

const handleMentorSelected = (user: UserVO) => {
  formData.value.mentorId = user.id
  formData.value.mentorCode = user.jobCode
  formData.value.mentorName = user.nickname
}

const handleAuditFinish = () => {
  edit.value = false
  dialogVisible.value = false
  emit('success')
}
</script>
